/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2025. All rights reserved.
 * Description: Hmfs-tools mkfs main writer
 */

#ifndef MAIN_WRITER_H
#define MAIN_WRITER_H

#include <memory>
#include <string>
#include <sys/types.h>

#include "hmfs_command.h"
#include "hmfs_fs.h"

namespace OHOS {
namespace Hmfs {
union NodeOnDisk {
    NodeData node;
    uint8_t size[HMFS_BLOCK_SIZE];
};

class MkfsFormat;
class MainAreaWriter {
public:
    MainAreaWriter(MkfsFormat &mkfs);
    ~MainAreaWriter() = default;

    int32_t Format();

private:
    MkfsFormat &mkfs_;
    std::unique_ptr<NodeOnDisk> rootInode_ = nullptr;
    std::unique_ptr<DentryBlock> dentryBlk_ = nullptr;

    int32_t PrepareRootInode();
    int32_t WriteRootInode();
    int32_t WriteLpfInode();
    uint32_t WriteDefaultLpfDentry();
    int32_t FillLpfInode(NodeData *lpfInode);
    int32_t DiscardObsoleteDnode();
    int32_t PrepareDefaultDentryRoot();
    int32_t WriteDentryBlock();
    uint64_t GetTimeStamp();
    int32_t WriteQfInodes();
    int32_t WriteQfInode(int32_t qtype, uint32_t offset);
    int32_t WriteDefaultQuotaData(int32_t qtype, uint64_t dataBlkId, uint32_t id);
};

} // namespace Hmfs
} // namespace OHOS
#endif // MAIN_WRITER_H
